﻿using System;
using System.Security;
using System.Security.Permissions;


namespace Helper
{
    [AttributeUsageAttribute(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
    [Serializable]
    sealed public class ObjectPermissionAttribute : CodeAccessSecurityAttribute
    {
        private bool unrestricted = false;
        private UserRoles permAccess;
        public ObjectPermissionAttribute(SecurityAction action)
            : base(action)
        {
            if (action == SecurityAction.Demand)
            {
                CreatePermission().Demand();
            }
        }

        public UserRoles RequiredPermissionAccess
        {
            set { permAccess = value; }
            get { return permAccess; }
        }

        public override IPermission CreatePermission()
        {
            if (unrestricted)
            {
                throw new ArgumentException("Unrestricted permissions not allowed in identity permissions.");
            }
            else
            {
                return new ObjectPermission(RequiredPermissionAccess);
            }
        }
    }
}
